﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HM.CS;
using HM.TESTCS;

namespace HM
{
    public partial class OutStockBack : Form
    {
        public InStockSrcBill srcOBJ
        {
            get;
            set;
        }

        public int RowCount
        {
            get;
            set;
        }

        public Boolean bRefresh
        {
            set
            {
                if (srcOBJ != null)
                {
                    this.txtSrcBillNo.Text = srcOBJ.BillNo;
                    this.lblSrcTypeID.Text = srcOBJ.BillTypeID; //隐藏控件,将源单类型的值给赋入
                    FillListView(new ApploginTest().GetSrcInfoBySrcNo(this.txtSrcBillNo.Text, lblSrcTypeID.Text));
                }
            }
        }

        public static OutStockBack sfrmInStock = null;

        public OutStockBack()
        {
            InitializeComponent();
            srcOBJ = new InStockSrcBill();
            sfrmInStock = this;
            SetFormWithAndHeith(this);
        }

        private void btnSourceBill_Click(object sender, EventArgs e)
        {
            InStockSourceBill frm = new InStockSourceBill();
            frm.sourceType = 1;
            frm.Show();
        }

        private void btnCommit_Click(object sender, EventArgs e)
        {
            string billNo;
            DataTable dt;
            
            billNo = this.txtSrcBillNo.Text.Trim();
            if (string.IsNullOrEmpty(billNo))
            {
                MessageBox.Show("请输入源单号!");
            }
            dt = new ApploginTest().GetSrcInfoBySrcNo(billNo, lblSrcTypeID.Text);
            FillListView(dt);
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            int intSelectIndex;
            string sRow;
            int keyIndex;
            string useStockSite;
            //InStockSrcBillDetail objDetail;

            if (lvInStockDetail.SelectedIndices.Count < 1)
            {
                MessageBox.Show("请选择一条数据");
                return;
            }

            intSelectIndex = lvInStockDetail.SelectedIndices[0];


            keyIndex = lvInStockDetail.Columns.IndexOf(colRow);
            txtNo.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colItemNumber);
            lblHidSrcItemNumber.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colBatch);
            txtBatch.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colStockID);
            txtStockID.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colStockName);
            txtStockName.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colUseStockSite);
            useStockSite= lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            if (useStockSite == "1")
            {
                //启用仓位管理
                keyIndex = lvInStockDetail.Columns.IndexOf(colStockSiteID);
                txtStockSiteID.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
                keyIndex = lvInStockDetail.Columns.IndexOf(colStockSiteName);
                txtStockSiteName.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            }
            else
            {
                //仓位不可编辑
                txtStockSiteID.ReadOnly = true;
            }
            keyIndex = lvInStockDetail.Columns.IndexOf(colSupplyID);
            txtSupplyID.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colSupplyName);
            txtSupplyName.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colUnitID);
            txtUnitID.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colUnitName);
            txtUnitName.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvInStockDetail.Columns.IndexOf(colQTYScan);
            txtCount.Text = lvInStockDetail.Items[intSelectIndex].SubItems[keyIndex].Text;

            tblInfo.SelectedIndex = 1;
            txtBarCode.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void InStock_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void FillListView(DataTable dtSrcDetail)
        {
            int rowNo;   //定义行号
            rowNo = 0; 
            try
            {
                if (dtSrcDetail.Rows.Count > 0)
                {

                    foreach (DataRow dr in dtSrcDetail.Rows)
                    {
                        string srcBillNo;
                        string itemID;
                        string itemName;
                        string itemNumber;
                        string batch;
                        string stockID;
                        string stockName;
                        string stockSiteID;
                        string stockSiteName;
                        string supplyID;
                        string supplyName;
                        string unitID;
                        string unitName;
                        string QTY;
                        string QTYScan;
                        string useStockSite;
                        string srcBillTypeID;
                        string srcBillID;
                        string srcBillEntryID;

                        rowNo = rowNo + 1;
                        RowCount = rowNo;
                        srcBillNo = dr["srcBillNo"].ToString();
                        itemID = dr["itemID"].ToString();
                        itemName = dr["itemName"].ToString();
                        itemNumber = dr["itemNumber"].ToString();
                        batch = dr["batch"].ToString();
                        stockID = dr["stockID"].ToString();
                        stockName = dr["stockName"].ToString();
                        stockSiteID = dr["stockSiteID"].ToString();
                        stockSiteName = dr["stockSiteName"].ToString();
                        supplyID = dr["supplyID"].ToString();
                        supplyName = dr["supplyName"].ToString();
                        unitID = dr["unitID"].ToString();
                        unitName = dr["unitName"].ToString();
                        QTY = dr["QTY"].ToString();
                        QTYScan = dr["QTYScan"].ToString();
                        useStockSite = dr["UseStockSite"].ToString();
                        srcBillTypeID = dr["srcBillTypeID"].ToString();
                        srcBillID = dr["srcBillID"].ToString();
                        srcBillEntryID = dr["srcBillEntryID"].ToString();

                        ListViewItem lvi = new ListViewItem();
                        //首列赋值
                        lvi.Text = rowNo.ToString();
                        lvi.ImageIndex = 0;
                        lvi.SubItems.Add(srcBillNo);
                        lvi.SubItems.Add(itemName);
                        lvi.SubItems.Add(itemNumber);
                        lvi.SubItems.Add(batch);
                        lvi.SubItems.Add(stockName);
                        lvi.SubItems.Add(stockSiteName);
                        lvi.SubItems.Add(supplyName);
                        lvi.SubItems.Add(unitName);
                        lvi.SubItems.Add(QTY);
                        lvi.SubItems.Add(QTYScan);
                        lvi.SubItems.Add(itemID);
                        lvi.SubItems.Add(stockID);
                        lvi.SubItems.Add(stockSiteID);
                        lvi.SubItems.Add(supplyID);
                        lvi.SubItems.Add(unitID);
                        lvi.SubItems.Add(useStockSite);
                        lvi.SubItems.Add(srcBillTypeID);
                        lvi.SubItems.Add(srcBillID);
                        lvi.SubItems.Add(srcBillEntryID);

                        lvInStockDetail.Items.Add(lvi);
                    }
                    //结束数据处理，UI界面一次性绘制。
                    lvInStockDetail.EndUpdate();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("抱歉,错误的原因是:" + e.ToString());
            }
        }

        /// <summary>
        /// 判断是否是合法的数字
        /// </summary>
        /// <param name="numberValue">字符串</param>
        /// <returns></returns>
        public bool checkNumber(string numberValue)
        {
            decimal dec;
            bool returnValue;
            returnValue = true;
            try
            {
                dec = Convert.ToDecimal(numberValue);
            }
            catch 
            {
                returnValue = false;
            }

            return returnValue;

        }

        public void ScanSend()
        {
            InStockSrcBillDetail objDetail;
            //检验传递的信息
            if (string.IsNullOrEmpty(txtItemNumber.Text.Trim()))
            {
                MessageBox.Show("请输入条码信息");
                txtBarCode.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtStockID.Text.Trim()))
            {
                MessageBox.Show("请输入仓库信息");
                txtStockID.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtStockSiteID.Text.Trim()) && lblHideUseStockSite.Text == "1")
            {
                MessageBox.Show("请输入仓位信息");
                txtStockSiteID.ReadOnly = false;
                txtStockSiteID.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtSupplyID.Text.Trim()))
            {
                MessageBox.Show("请输入供应商信息");
                txtSupplyID.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtCount.Text.Trim()))
            {
                MessageBox.Show("请输入数量信息");
                txtCount.Focus();
                return;
            }

            if (txtCount.Text.Trim() == "0")
            {
                MessageBox.Show("请输入数量信息");
                txtCount.Focus();
                return;
            }

            
            //处理非法数字
            if (!checkNumber(txtCount.Text.Trim()))
            {
                MessageBox.Show("输入数量格式有误,请重新输入");
                txtCount.Text = "0";
                txtCount.Focus();
                return;
            }

            objDetail = new InStockSrcBillDetail();
            objDetail.itemNumber = txtItemNumber.Text.Trim();
            objDetail.itemName = txtItemName.Text.Trim();
            objDetail.batch = txtBatch.Text.Trim();
            objDetail.stockID = txtStockID.Text.Trim();
            objDetail.stockName = txtStockName.Text.Trim();
            objDetail.stockSiteID = txtStockSiteID.Text.Trim();
            objDetail.stockSiteName = txtStockSiteName.Text.Trim();
            objDetail.supplyID = txtSupplyID.Text.Trim();
            objDetail.supplyName = txtSupplyName.Text.Trim();
            objDetail.unitID = txtUnitID.Text.Trim();
            objDetail.unitName = txtUnitName.Text.Trim();
            objDetail.QTYScan = txtCount.Text.Trim();

            int RowNo;
            if (!string.IsNullOrEmpty(txtNo.Text))
            {
                RowNo = Convert.ToInt32(txtNo.Text);
            }
            else
            {
                RowNo = 0;
            }

            try
            {
                EditSrcBillDetail(RowNo, objDetail);
                MessageBox.Show("扫描成功");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        public void EditSrcBillDetail(int rowNo, InStockSrcBillDetail obj)
        {
            //rowNo 为0 新增无关联源单数据
            int keyIndex;
            if (rowNo > 0)
            {
                //修改数据
                keyIndex = lvInStockDetail.Columns.IndexOf(colBatch);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.batch;
                keyIndex = lvInStockDetail.Columns.IndexOf(colStockID);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.stockID;
                keyIndex = lvInStockDetail.Columns.IndexOf(colStockName);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.stockName;
                keyIndex = lvInStockDetail.Columns.IndexOf(colStockSiteID);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.stockSiteID;
                keyIndex = lvInStockDetail.Columns.IndexOf(colStockSiteName);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.stockSiteName;
                keyIndex = lvInStockDetail.Columns.IndexOf(colSupplyID);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.supplyID;
                keyIndex = lvInStockDetail.Columns.IndexOf(colSupplyName);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.supplyName;
                keyIndex = lvInStockDetail.Columns.IndexOf(colUnitID);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.unitID;
                keyIndex = lvInStockDetail.Columns.IndexOf(colUnitName);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.unitName;
                keyIndex = lvInStockDetail.Columns.IndexOf(colQTYScan);
                lvInStockDetail.Items[rowNo - 1].SubItems[keyIndex].Text = obj.QTYScan;
            }
            else
            {
                obj.srcBillTypeID = "";
                obj.srcBillID = "";
                obj.srcBillEntryID = "";

                ListViewItem lvi = new ListViewItem();
                //首列赋值
                lvi.Text = (RowCount + 1).ToString();
                lvi.ImageIndex = 0;
                lvi.SubItems.Add(obj.srcBillNo);
                lvi.SubItems.Add(obj.itemName);
                lvi.SubItems.Add(obj.itemNumber);
                lvi.SubItems.Add(obj.batch);
                lvi.SubItems.Add(obj.stockName);
                lvi.SubItems.Add(obj.stockSiteName);
                lvi.SubItems.Add(obj.supplyName);
                lvi.SubItems.Add(obj.unitName);
                lvi.SubItems.Add(obj.QTY);
                lvi.SubItems.Add(obj.QTYScan);
                lvi.SubItems.Add(obj.itemID);
                lvi.SubItems.Add(obj.stockID);
                lvi.SubItems.Add(obj.stockSiteID);
                lvi.SubItems.Add(obj.supplyID);
                lvi.SubItems.Add(obj.unitID);
                lvi.SubItems.Add(obj.useStockSite);
                lvi.SubItems.Add(obj.srcBillEntryID);
                lvi.SubItems.Add(obj.srcBillID);
                lvi.SubItems.Add(obj.srcBillEntryID);

                lvInStockDetail.Items.Add(lvi);
            }
        }

        private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            string itemNumber;
            string itemName;
            string useStockSite;
            if (e.KeyChar == (char)13)
            {
                if (!string.IsNullOrEmpty(txtBarCode.Text.Trim()))
                {
                    DataTable dt = new ApploginTest().getBarCode(txtBarCode.Text.Trim());
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            itemNumber = dr["itemNumber"].ToString();
                            itemName = dr["itemName"].ToString();
                            useStockSite = dr["useStockSite"].ToString();

                            if (!string.IsNullOrEmpty(lblHidSrcItemNumber.Text))
                            {
                                //有源单
                                if (itemNumber != lblHidSrcItemNumber.Text)
                                {
                                    MessageBox.Show("该条码和源单物料信息不匹配,请确认");
                                    return;
                                }
                                else
                                {
                                    txtItemNumber.Text = itemNumber;
                                    txtItemName.Text = itemName;
                                    txtBarCode.Text = "";
                                    if (useStockSite == "1")
                                    {
                                        lblHideUseStockSite.Text = "1";
                                        txtStockSiteID.ReadOnly = false;
                                    }
                                }
                            }
                            else
                            {
                                //无源单
                                txtItemNumber.Text = itemNumber;
                                txtItemName.Text = itemName;
                                txtBarCode.Text = "";
                                if (useStockSite == "1")
                                {
                                    lblHideUseStockSite.Text = "1";
                                    txtStockSiteID.ReadOnly = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("条码信息不存在");
                        txtBarCode.Text = "";
                        txtBarCode.Focus();
                    }
                }
            }

        }

        private void txtStockID_KeyPress(object sender, KeyPressEventArgs e)
        {
            string stockName = "";
            if (e.KeyChar == (char)13)
            {
                if (!string.IsNullOrEmpty(txtStockID.Text.Trim()))
                {
                    //stockName = new ApploginTest().GetStockName(txtStockID.Text.Trim());
                }

                if (!string.IsNullOrEmpty(stockName))
                {
                    txtStockName.Text = stockName;
                    txtSupplyID.Focus();
                }
                else
                {
                    MessageBox.Show("仓库不存在");
                    
                    txtStockID.Focus();
                    txtStockID.Text = "";
                    txtStockName.Text = "";
                }
            }
        }

        private void txtStockSiteID_KeyUp(object sender, KeyEventArgs e)
        {
            string stockSiteName = "";

            if (string.IsNullOrEmpty(txtStockID.Text.Trim()))
            {
                MessageBox.Show("请确定仓库信息!");

                txtStockID.Focus();
                txtStockSiteID.Text = "";
                txtStockSiteName.Text = "";
            }

            if (e.KeyValue == 13)
            {
                if (!string.IsNullOrEmpty(txtStockSiteID.Text.Trim()))
                {
                    stockSiteName = new ApploginTest().GetStockSiteName(txtStockID.Text.Trim(), txtStockSiteID.Text.Trim());
                }

                if (!string.IsNullOrEmpty(stockSiteName))
                {
                    txtStockSiteName.Text = stockSiteName;
                    txtSupplyID.Focus();
                }
                else
                {
                    MessageBox.Show("仓位不存在");
                    txtStockSiteID.Text = "";
                    txtStockSiteName.Text = "";
                    txtSupplyID.Focus();
                }
            }
        }


        private void txtSupplyID_KeyPress(object sender, KeyPressEventArgs e)
        {
            string supplyName = "";
            if (e.KeyChar == (char)13)
            {
                if (!string.IsNullOrEmpty(txtSupplyID.Text.Trim()))
                {
                    supplyName = new ApploginTest().GetSupplyName(txtSupplyID.Text.Trim());
                }

                if (!string.IsNullOrEmpty(supplyName))
                {
                    txtSupplyName.Text = supplyName;
                    txtUnitID.Focus();
                }
                else
                {
                    MessageBox.Show("供应商不存在");
                    txtSupplyID.Text = "";
                    txtSupplyName.Text = "";
                    txtUnitID.Focus();
                }
            }
        }

        private void txtUnitID_KeyPress(object sender, KeyPressEventArgs e)
        {
            string unitName = "";
            if (e.KeyChar == (char)13)
            {
                if (!string.IsNullOrEmpty(txtUnitID.Text.Trim()))
                {
                    unitName = new ApploginTest().GetUnitName(txtUnitID.Text.Trim());
                }

                if (!string.IsNullOrEmpty(unitName))
                {
                    txtUnitName.Text = unitName;
                    txtCount.Focus();
                }
                else
                {
                    MessageBox.Show("单位不存在");
                    txtUnitID.Text = "";
                    txtUnitName.Text = "";
                    txtUnitID.Focus();
                }
            }
        }

        private void txtCount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46 && (int)e.KeyChar != 13)
                e.Handled = true;

            //if ((int)e.KeyChar == 46)                           //小数点
            //{
            //    if (txtCount.Text.Length <= 0)
            //        e.Handled = true;   //小数点不能在第一位
            //    if (txtCount.Text.Substring()
            //}

            if ((int)e.KeyChar == 13)
            {
                ScanSend();
            }

        }

        private void btnNewAll_Click(object sender, EventArgs e)
        {
            txtNo.Text = "";
            txtBarCode.Text = "";
            txtItemNumber.Text = "";
            txtItemName.Text = "";
            txtBatch.Text = "";
            lblHidSrcItemNumber.Text = "";
            txtStockID.Text = "";
            txtStockName.Text = "";
            txtStockSiteID.Text = "";
            txtStockSiteID.ReadOnly = true;
            lblHideUseStockSite.Text = "0";
            txtStockSiteName.Text = "";
            txtSupplyID.Text = "";
            txtSupplyName.Text = "";
            txtUnitID.Text = "";
            txtUnitName.Text = "";
            txtCount.Text = "0";
            txtBarCode.Focus();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            ScanSend();
        }


        private void SetFormWithAndHeith(Form obj)
        {
            Rectangle ScreenArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            int width = ScreenArea.Width;
            int height = ScreenArea.Height;
            obj.Width = width - 5;
            obj.Height = height - 5;
        }

    }
}